﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class MultiChoiceFieldType : SimpleFieldType<IEnumerable<string>>
    {
        public MultiChoiceFieldType(Guid id) : base(id)
        {
        }

        public override IEnumerable<string> GetValue(SPListItem item)
        {
            var fieldValue = GetFieldValue<SPFieldMultiChoiceValue>(item);

            var values = new List<string>();
            for (int i = 0; i < fieldValue.Count; i++)
            {
                values.Add(fieldValue[i]);
            }

            return values;
        }

        public override void SetValue(SPListItem item, IEnumerable<string> values)
        {
            var fieldValue = new SPFieldMultiChoiceValue();
            if (values != null)
            {
                foreach (string value in values)
                {
                    fieldValue.Add(value);
                }
            }
            item[FieldId] = fieldValue;
        }
    }
}